/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package com.xunce.xcworkservice.utils;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.HashMap;
import java.util.Map;


public class HttpContextUtils {

	public static HttpServletRequest getHttpServletRequest() {
		return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
	}

	public static String getDomain(){
		HttpServletRequest request = getHttpServletRequest();
		StringBuffer url = request.getRequestURL();
		return url.delete(url.length() - request.getRequestURI().length(), url.length()).toString();
	}

	public static String getOrigin(){
		HttpServletRequest request = getHttpServletRequest();
		return request.getHeader("Origin");
	}

	public static Map<String, Object> getParamMap(HttpServletRequest request) {
		Map<String, Object> result = new HashMap<>();
		Map<String, String[]> map = request.getParameterMap();
		for (Map.Entry<String, String[]> e : map.entrySet()) {
			if (e.getValue() != null && e.getValue().length > 0) {
				result.put(e.getKey(), e.getValue()[0]);
			}
		}
		return result;
	}
}
